package com.intel.factorytest.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.intel.factorytest.R;
import com.intel.factorytest.TestFileOp;
import com.intel.factorytest.TestShell;

public class Illumination extends BaseActivity {

    private static final String TAG = "ShowBrightness";
    
    private TextView brightnessStatusText;
    public Timer timer = new Timer();
    private int tempBrightness;
    String title = "";
    //private TestFileOp mTestFile = new TestFileOp();
    private TestShell mTestShell = new TestShell();
    private int mBValue = 0;
    private int mCount = 0;
    private String SHOW_BRIGHTNESS_BayTrail = "cat /sys/class/backlight/intel_backlight/actual_brightness > ";
    private String SHOW_BRIGHTNESS_SOFIA = "cat /sys/class/leds/lcd-backlight/brightness > ";
    private String SHOW_BRIGHTNESS_CLV = "cat /sys/class/backlight/psb-bl/actual_brightness > ";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.brightness);
        initView();
        tempBrightness = getBrightness();
        brightnessStatusText = (TextView) findViewById(R.id.brightnessStatus);
        brightnessStatusText.setTextColor(Color.BLACK);
        brightnessStatusText.setTextSize(24);
        title = this.getResources()
                .getText(R.string.SERVICEMENU_ILLUMINATION_LEVEL_TXT)
                .toString();
        brightnessStatusText.setText(title + tempBrightness);

    }
    
    protected void initView() {
        super.initView();
        // FFD3D3D3
        mSucBtn.requestFocus();
        //remove by wangsm
    //    mSucBtn.setBackgroundColor(Color.argb(255, 211, 211, 211));
        //mFailBtn.setBackgroundColor(Color.argb(255, 211, 211, 211));
        //mRepeatBtn.setBackgroundColor(Color.argb(255, 211, 211, 211));
    }
    
    /*********************change brightness method 1*************************/
    private void changeBrightness()
    {
        TimerTask tTask  = new TimerTask()
        {
            public void run()
            {
                handler.sendEmptyMessage(0);
            }
        };
        timer.schedule(tTask, 500);
    }
    
    Handler handler = new Handler()   
    {
        public void handleMessage(Message msg)
        {
            mCount++;
            Log.d("LJK", "getBrightness() = "+getBrightness());
            tempBrightness = tempBrightness + 40;
            if (tempBrightness <= 250){
                setBrightness(tempBrightness);
                brightnessStatusText.setText(title + tempBrightness); 
                Log.i(TAG, "A " + tempBrightness);
                brightnessIsOk(tempBrightness);
            }else{
                tempBrightness = 20;
                setBrightness(tempBrightness);
                brightnessStatusText.setText(title + tempBrightness);
        brightnessIsOk(tempBrightness);
            }            
            Log.i(TAG, "tempBrightness: " + tempBrightness);
        if(mCount > 5) {
        if(is_pass) {
            brightnessStatusText.setText(title + tempBrightness + "\n" + Illumination.this.getResources().getText(R.string.SERVICEMENU_ILLUMINATION_RESULT_TXT).toString());
            
        } else {
            brightnessStatusText.setText(title + tempBrightness + "\n" + Illumination.this.getResources().getText(R.string.SERVICEMENU_ILLUMINATION_FAIL_RESULT_TXT).toString());
        }
        mItemTestFinish = true;//add by wangsm
        return;
        }
            changeBrightness();     
        }
    };    
    
   /* public boolean onTouchEvent(MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN )
        {
            showDialog(0);            
        }
        return true;
    }*/
    
    @Override
    protected Dialog onCreateDialog(int none)
    {
        return new AlertDialog.Builder(Illumination.this)
//        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle(R.string.SERVICEMENU_DIALOG_TITLE_TXT)
        .setPositiveButton(R.string.SERVICEMENU_DIALOG_YES_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {                
                             
                finish();
                
            }
        })
        .setNegativeButton(R.string.SERVICEMENU_DIALOG_NO_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                          
                finish();
            }
        })
        .create();
    }
    
    @Override
    protected void onPause()
    {        
        super.onPause();
    }
    @Override
    protected void onResume() {
        try 
           { 
               changeBrightness(); 
           }catch(Exception e)
           {
           } 
        super.onResume();
    }
    
    
    
    public int getBrightness(){    
        int brightness;
        try {
            brightness = Settings.System.getInt(this.getContentResolver(), 
                Settings.System.SCREEN_BRIGHTNESS);
        } catch (SettingNotFoundException snfe) {
            brightness = 250;
        }
        Log.v(TAG,"brightness="+brightness);
        return brightness;
    }
    
    public void setBrightness(int brightness){
        WindowManager.LayoutParams lp = getWindow().getAttributes(); 
        lp.screenBrightness = brightness / 250.0f; 
        getWindow().setAttributes(lp);
    }
    
    @Override
    protected void onStop() {
        if(timer!=null){
            timer.cancel();
        }
        if (handler != null) {
            handler.removeMessages(0);    
        }
        super.onStop();
    }

    @Override
    public void finish() {
        tc.setTestResult(is_pass);
        super.finish();
    }
    void brightnessIsOk(int brightness) {
        //int sys_test = mTestFile.ReadTextFile(); 
        /*if (isSoFIAProduct()) {
            mTestShell.callShellCommand(SHOW_BRIGHTNESS_SOFIA);
        } else {
            mTestShell.callShellCommand(SHOW_BRIGHTNESS_BayTrail);
        }
        if(mTestShell.mCatchException) {
            mTestShell.callShellCommand(SHOW_BRIGHTNESS_CLV);
        }
        int b_value = mTestFile.ReadTextFile(); */
        int b_value = brightness;
        //Log.e(TAG,"### b_value ="+b_value);
        if (b_value >0 && b_value < 255 ) {
            if(mBValue != b_value) {
                is_pass = true;
            }
        }else {
            is_pass = false;
        }
        mBValue = b_value;
        //mTestFile.WriteTextFile(sys_test);
        //        finish();
    }
    public boolean ac_ispass() {
        return is_pass;
    } 
}
